package com.dao;

import org.hibernate.*;

import com.listener.HibernateListener;
import com.pojo.Contact;

import java.util.ArrayList;
import java.util.List;

public class ContactDAO {

	public void insertContact(Contact contact) {
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(contact);
		transaction.commit();
		session.close();
	}

	public List<Contact> list(String isActive) {
		@SuppressWarnings("static-access")
		Session session = new HibernateListener().getSessionFactory()
				.openSession();
		session.beginTransaction();
		List<Contact> list = null;
		String sql;
		if (isActive.equals("No")) {
			sql = "from Contact";
		} else {
			sql = "from Contact where isActive like '" + isActive + "'";
		}
		Query query = session.createQuery(sql);
		list = query.list();
		for (Contact c : list) {
			c.setFullName(c.getFirstName() + " " + c.getSname());
		}
		session.getTransaction().commit();
		session.close();
		return list;
	}

	public void updateContact(Contact contact, int contactId) {
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Contact c = (Contact) session
				.get(Contact.class, contact.getContactId());
		transaction.commit();
		session.close();
		c.setFirstName(contact.getFirstName());
		c.setSname(contact.getSname());
		c.setKnowAs(contact.getKnowAs());
		c.setOfficePhone(contact.getOfficePhone());
		c.setBestContactMethod(contact.getBestContactMethod());
		c.setMobilePhone(contact.getMobilePhone());
		c.setHomePhone(contact.getHomePhone());
		c.setEmailAddr(contact.getEmailAddr());
		c.setContactType(contact.getContactType());
		c.setJobRole(contact.getJobRole());
		c.setWorkBase(contact.getWorkBase());
		c.setJobTitle(contact.getJobTitle());
		c.setIsActive(contact.getIsActive());
		c.setManagerName(contact.getManagerName());
		Session session2 = HibernateListener.getSessionFactory().openSession();
		transaction = session2.beginTransaction();

		session2.update(c);
		transaction.commit();
		session2.close();
	}

	@SuppressWarnings("unchecked")
	public Contact getContactByContactNumber(String mobilePhone) {
		String sql = "select new com.pojo.Contact(a.contactId, a.firstName, a.sname, a.knowAs, a.officePhone, a.mobilePhone, a.homePhone, a.emailAddr,"
				+ "a.contactType, a.bestContactMethod, a.jobRole, a.workBase, a.jobTitle, a.isActive, a.managerName, a.fullName) "
				+ " from Contact as a where a.mobilePhone='"
				+ mobilePhone
				+ "'";
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Query query = session.createQuery(sql);
		List<Contact> List = query.list();
		transaction.commit();
		return List.get(0);

	}

	@SuppressWarnings("unchecked")
	public Contact getContactById(int contactID) {
		String sql = "from Contact where ContactID=" + contactID;
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Query query = session.createQuery(sql);
		List<Contact> List = query.list();
		transaction.commit();
		return List.get(0);
	}

	public void deleteContact(int contactId) {
		Contact contact = getContactById(contactId);
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Contact c = new Contact();
		c.setContactId(contactId);
		c.setFirstName(contact.getFirstName());
		c.setSname(contact.getSname());
		c.setKnowAs(contact.getKnowAs());
		c.setOfficePhone(contact.getOfficePhone());
		c.setBestContactMethod(contact.getBestContactMethod());
		c.setMobilePhone(contact.getMobilePhone());
		c.setHomePhone(contact.getHomePhone());
		c.setEmailAddr(contact.getEmailAddr());
		c.setContactType(contact.getContactType());
		c.setJobRole(contact.getJobRole());
		c.setWorkBase(contact.getWorkBase());
		c.setJobTitle(contact.getJobTitle());
		c.setIsActive(contact.getIsActive());
		c.setManagerName(contact.getManagerName());
		session.delete(c);
		transaction.commit();
		session.close();

	}

	@SuppressWarnings("unchecked")
	public List<Contact> getByFilterName(String filterName, String isActive) {
		List<Contact> list = new ArrayList<Contact>();
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		String sql;
		if (isActive.equals("No")) {
			sql = "from Contact where FirstName like '" + filterName + "%'";
		} else {
			sql = " from Contact  where FirstName like '" + filterName
					+ "%' and isActive like '" + isActive + "'";
		}
		Query query = session.createQuery(sql);
		list = query.list();
		for (Contact c : list) {
			c.setFullName(c.getFirstName() + " " + c.getSname());
		}
		transaction.commit();
		session.close();
		return list;
	}
}
